package com.example.bnuzzu.controller;


import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bnuzzu.common.dto.OrderListDto;
import com.example.bnuzzu.common.lang.Result;
import com.example.bnuzzu.entity.Houseinfo;
import com.example.bnuzzu.entity.Orderinfo;
import com.example.bnuzzu.entity.Userinfo;
import com.example.bnuzzu.service.FavoritehouseService;
import com.example.bnuzzu.service.HouseinfoService;
import com.example.bnuzzu.service.OrderinfoService;
import com.example.bnuzzu.service.UserinfoService;
import com.example.bnuzzu.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BNUZZU
 * @since 2021-04-13
 */
@RestController
public class OrderinfoController {
    @Autowired
    OrderinfoService orderinfoService;
    @Autowired
    HouseinfoService houseinfoService;
    @Autowired
    UserinfoService userinfoService;
    @Autowired
    FavoritehouseService favoritehouseService;

    /**
     * 增加/修改订单
     * @param orderinfo order信息
     * @return result
     */
    @PostMapping(value = "/OrderinfoEdit")
    public Result editOrder(@RequestBody Orderinfo orderinfo) {
        if(orderinfo.getTenantid() != null){
            Assert.isTrue(orderinfo.getTenantid() == ShiroUtil.getProfile().getUserid().intValue());
        }else{
            return Result.fail(500,"请重新登录",null);
        }

        if(orderinfo.getTenantid().equals(orderinfo.getOwnerid())){
            return Result.fail(500,"无法申请为自己的房客",null);
        }

        if(orderinfoService.getById(orderinfo) != null){
            return Result.fail(500,"无法重复提交申请",null);
        }

        orderinfoService.saveOrUpdate(orderinfo);

        return  Result.success(null);
    }

    @GetMapping(value = "/getMyOrders/{id}")
    public Result getOrder(@PathVariable(name = "id") Integer userid){
        if(userid != null){
            Assert.isTrue(userid == ShiroUtil.getProfile().getUserid().intValue());
        }else{
            return Result.fail(500,"请重新登录",null);
        }

        List<OrderListDto> TenantOrders = orderinfoService.selectTenantOrderInfos(userid);
        List<OrderListDto> OwnerOrders = orderinfoService.selectOwnOrderInfos(userid);

        Map<String,Object> map = new LinkedHashMap<>();
        map.put("TenantOrders",TenantOrders);
        map.put("OwnerOrders",OwnerOrders);

        return Result.success(map);
    }

    @PostMapping("/agreeOrder")
    public Result agree(@RequestBody Orderinfo orderinfo){
        if(orderinfo.getOwnerid() != null){
            Assert.isTrue(orderinfo.getOwnerid() == ShiroUtil.getProfile().getUserid().intValue());
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        orderinfo.setOrderstatus(1);
        orderinfoService.updateById(orderinfo);

        QueryWrapper<Orderinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("houseid",orderinfo.getHouseid())
                .eq("orderstatus",1);
        if(orderinfoService.count(wrapper)+1 == houseinfoService.getById(orderinfo.getHouseid()).getLimited()) {
            Houseinfo houseinfo = new Houseinfo();
            houseinfo.setHouseid(orderinfo.getHouseid());
            houseinfo.setHousestate(1);
            houseinfoService.updateById(houseinfo);
        }else if(orderinfoService.count(wrapper)+1 > houseinfoService.getById(orderinfo.getHouseid()).getLimited()){
            return Result.fail(500,"已大于上限人数",null);
        }

        return Result.success(null);
    }

    @PostMapping("/denyOrder/{mode}")
    public Result deny(@RequestBody Orderinfo orderinfo, @PathVariable(name = "mode") Integer mode) {
        //mode 1房东拒绝 2租客撤销
        if (mode == 1) {
            if (orderinfo.getOwnerid() != null) {
                Assert.isTrue(orderinfo.getOwnerid() == ShiroUtil.getProfile().getUserid().intValue());
            } else {
                return Result.fail(500, "请重新登录", null);
            }
            orderinfo.setOrderstatus(2);
            orderinfoService.updateById(orderinfo);

            return Result.success(null);

        } else if (mode == 2) {
            if (orderinfo.getTenantid() != null) {
                Assert.isTrue(orderinfo.getTenantid() == ShiroUtil.getProfile().getUserid().intValue());
            } else {
                return Result.fail(500, "请重新登录", null);
            }
            orderinfo.setOrderstatus(2);
            orderinfoService.updateById(orderinfo);

            return Result.success(null);
        }

        return Result.fail(500,"null",null);
    }


    /**
     * 根据订单表ID删除订单
     * @param orderinfo 信息
     * @return
     */
    @PostMapping(value = "/OrderinfoDel")
    public Result deleteOrder(@RequestBody String orderinfo) {

        int orderIDs = Integer.parseInt(JSONObject.parseObject(orderinfo).get("OrderID").toString());
        Long orderID = new Long(orderIDs);

        int i = orderinfoService.deleteOrderinfoById(orderID);
        if(i>0) {
            return Result.success("成功");
        } else {
            return Result.success("失败");
        }
    }

    /**
     * 根据订单ID查询订单
     * @return
     */
    @PostMapping(value = "/OrderinfoSelectByOrderID")
    public Result orderSelectByOrderID(@RequestBody String tenant) {

        int OrderID = Integer.parseInt(JSONObject.parseObject(tenant).get("OrderID").toString());

        Long orderID = new Long(OrderID);

        Orderinfo orderinfo = orderinfoService.selectOrderinfoById(orderID);

        return Result.success(orderinfo);

    }

    /**
     * 管理员查看所有订单
     * @param
     * @return
     */
    @PostMapping(value = "/OrderinfoSelectAdmin")
    public Result selectOrderinfoList(@RequestBody Orderinfo orderinfo) {
//        Orderinfo orderinfo = new Orderinfo();

        List<Orderinfo> orderinfos = orderinfoService.selectOrderinfoList(orderinfo);
        return Result.success(orderinfos);

    }





}

